package bbs2.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import bbs2.model.ArticleCategory;

public class ArticleCategoryDAO {
    
    private static ArticleCategory makeArticleCategory(ResultSet rs) throws Exception {
        ArticleCategory category = new ArticleCategory();
        category.setId(rs.getInt("ID"));
        category.setBoardId(rs.getInt("boardID"));
        category.setName(rs.getString("name"));
        return category;
    }
    
    public static ArrayList<ArticleCategory> selectAll(int boardId) throws Exception {
        ArrayList<ArticleCategory> list = new ArrayList<ArticleCategory>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;        
        try {
            connection = DB.getConnection();
            String sql = "SELECT * FROM [ArticleCategory] WHERE [boardID] = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, boardId);
            resultSet = statement.executeQuery();
            while (resultSet.next())
                list.add(makeArticleCategory(resultSet));
        } finally {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connection != null) connection.close();
        }
        return list;
    } 
    
}
